package net.liteheaven.dtimer.timer

import android.annotation.TargetApi
import android.app.job.JobInfo
import android.app.job.JobScheduler
import android.content.ComponentName
import android.content.Context
import android.content.Context.JOB_SCHEDULER_SERVICE
import android.os.Build
import android.os.PersistableBundle
import net.liteheaven.dtimer.XLog

/**
 * Created by LiangYu on 2018/7/27.
 */
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
object JobUtil {

    fun scheduleJob(ctx: Context, jobId: Int, delayMs: Long, bundle: PersistableBundle) {
        XLog.i("JobUtil scheduleBroadcast delay = $delayMs")

        val jobInfo = JobInfo.Builder(jobId, ComponentName(ctx.packageName, DTimerJobService::class.java.name))
                /*.setPeriodic(1000)*/
                .setMinimumLatency(delayMs)
                .setOverrideDeadline(delayMs)
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
                .setExtras(bundle)
                .build()
        val js : JobScheduler = ctx.getSystemService(JOB_SCHEDULER_SERVICE) as JobScheduler
        js.schedule(jobInfo)
    }

    fun cancelJob(ctx: Context, jobId: Int){
        val js : JobScheduler = ctx.getSystemService(JOB_SCHEDULER_SERVICE) as JobScheduler
        js.cancel(jobId)
    }
}